﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：TongTongMallExceptionFilterAttribute.cs
// 功能描述：TongTongMall.WebApi
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using Castle.Core.Logging;
using System.Net;
using System.Net.Http;
using System.Runtime.Remoting.Messaging;
using System.Threading.Tasks;
/*----------------------------------------------------------------
// Copyright (C) 2016 通通优品
// 版权所有。
//
// 类名：TongTongMallExceptionFilterAttribute
// 功能描述：全局异常接收器
// 
// Creator by jym
//----------------------------------------------------------------*/
using System.Web.Http.Filters;
using TongTongMall.Dependecy;
using TongTongMall.Json;
using TongTongMall.Logging;
using TongTongMall.WebApi.Models;

namespace TongTongMall.WebApi.WebApi.Controllers.Filters
{
    public class TongTongMallExceptionFilterAttribute : ExceptionFilterAttribute, ITransientDependency
    {
        public ILogger Logger { get; set; }

        public TongTongMallExceptionFilterAttribute()
        {
            Logger = NullLogger.Instance;
        }

        public override void OnException(HttpActionExecutedContext context)
        {
            var wrapResultAttribute = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(context.ActionContext.ActionDescriptor);
            //判断返回的ajax类型
            if (wrapResultAttribute == null)
            {
                var returnType = context.ActionContext.ActionDescriptor.ReturnType;
                if (returnType == typeof(AjaxResponse) || returnType == typeof(Task<AjaxResponse>))
                {
                    wrapResultAttribute = WrapResultAttribute.Default;
                }
                else
                {
                    wrapResultAttribute = new WrapResultAttribute(false, false);
                }
            }
            if (wrapResultAttribute.LogError)
            {
                string parmeters = string.Empty;
                if (CallContext.HostContext is System.Web.HttpContext)
                {
                    var httpContext = CallContext.HostContext as System.Web.HttpContext;
                    if (httpContext.Request.ContentLength > 0 && httpContext.Request?.InputStream.Length > 0)
                    {
                        byte[] stream = new byte[httpContext.Request.InputStream.Length];
                        System.Web.HttpContext.Current.Request.InputStream.Read(stream, 0, stream.Length);
                        parmeters = "POST：" + System.Text.Encoding.Default.GetString(stream) + "\r\nGet:" + httpContext.Request.QueryString.ToString() 
                            + "\r\nIP:" + httpContext.Request.UserHostAddress;
                    }
                }
                LogHelper.LogException(Logger, context.Exception, parmeters);
            }
            //包装错误内容
            if (wrapResultAttribute.WrapOnError)
            {
                context.Response = context.Request.CreateResponse(
                    HttpStatusCode.OK,
                    new AjaxResponse(
                        ErrorInfoBuilder.Instance.BuildForException(context.Exception.GetBaseException()), false)
                    );
            }
            else
            {
                context.Response = context.Request.CreateResponse(
                    HttpStatusCode.InternalServerError,
                    ErrorInfoBuilder.Instance.BuildForException(context.Exception.GetBaseException()));
            }
        }
    }
}
